package com.example.b_308.myapplication.view.main;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.comment.utils.Global;
import com.example.b_308.myapplication.comment.view.page.WorkPageAdapter;
import com.example.b_308.myapplication.comment.view.page.WorkTabPageFragmentAdapter;
import com.example.b_308.myapplication.comment.view.page.WorkViewPager;
import com.example.b_308.myapplication.model.User;
import com.example.b_308.myapplication.view.base.BaseActivity;
import com.example.b_308.myapplication.view.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity {
    WorkViewPager workViewPager;
    RadioGroup rg;
    List<BaseFragment> fragments = new ArrayList<>();
    int current = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        Global.init(this);
        Global.setNoStatusBarFullMode(this);
        setContentView(R.layout.activity_main);
        getTitleBar().getTv_setting().setTextColor(Global.getColor(R.color.color_bg));
        workViewPager = (WorkViewPager) findViewById(R.id.work_view_page);
        rg = (RadioGroup) findViewById(R.id.rg_bottom_tab);
        /*tentForResult(RegisterActivity.class, 1);*/

        fragments.add(new MessageFragment().setTitle("企聊"));
        fragments.add(new AddressFragment().setTitle("通讯录"));
        fragments.add(new WorkFragment().setTitle("工作台"));
        fragments.add(new MineFragment().setTitle("我的"));

        getTitleBar().setTitle(Global.getString(R.string.qiliao));
        getTitleBar().getIv_return().setVisibility(View.GONE);
        getTitleBar().getTv_setting().setVisibility(View.GONE);
        getTitleBar().getIv_setting().setVisibility(View.GONE);

        workViewPager.setAdapter(new WorkPageAdapter(getSupportFragmentManager(), fragments));
        workViewPager.setCurrentItem(current);
        ((RadioButton) rg.getChildAt(current)).setChecked(true);

        workViewPager.setAdapter((new WorkTabPageFragmentAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
        }));
        workViewPager.setCurrentItem(current);
        workViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(current != position){
                ((RadioButton) rg.getChildAt(current)).setChecked(false);
                ((RadioButton) rg.getChildAt(position)).setChecked(true);
                    current=position;
                }
                /*fragments.get(position).setSelec(true);
                fragments.get(current).setSelec(false);
                current=position;*/
                switch (current){
                    case 0:
                        getTitleBar().setTitle(Global.getString(R.string.qiliao));
                        getTitleBar().getIv_return().setVisibility(View.GONE);
                        getTitleBar().getTv_setting().setVisibility(View.GONE);
                        getTitleBar().getIv_setting().setVisibility(View.GONE);
                        break;
                    case 1:
                        getTitleBar().setTitle(Global.getString(R.string.address));
                        getTitleBar().getIv_return().setVisibility(View.GONE);
                        getTitleBar().getTv_setting().setVisibility(View.GONE);
                        getTitleBar().getIv_setting().setVisibility(View.GONE);
                        break;
                    case 2:
                        getTitleBar().setTitle(Global.getString(R.string.w_tab));
                        getTitleBar().getIv_return().setVisibility(View.GONE);
                        getTitleBar().getTv_setting().setVisibility(View.GONE);
                        getTitleBar().getIv_setting().setVisibility(View.GONE);
                        break;
                    case 3:
                        getTitleBar().setTitle(Global.getString(R.string.mine));
                        getTitleBar().getIv_return().setVisibility(View.GONE);
                        getTitleBar().setSetting(Global.getString(R.string.exit));
                        getTitleBar().getIv_setting().setVisibility(View.GONE);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        workViewPager.setCurrentItem(0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(1 == requestCode){
            if(3 == requestCode){
                User user = (User) data.getSerializableExtra("user");
            }
        }
    }

    public void check(View view) {
       int index= rg.indexOfChild(view);
        workViewPager.setCurrentItem(index);
    }

    @Override
    public void fail(String message) {

    }
}
